package view;

import controller.UserController;
import dao.AddressDao;
import dao.impl.AddressDaoImpl;
import model.Address;
import model.User;
import model.common.Entity;

import java.util.Date;
import java.util.List;

/**
 * @author yhl
 * @description 类说明
 * @date 2023-11-15 9:48
 * @email 2919886673@qq.com
 */
public class IndividualClient extends Client {


    public static final UserController userController = new UserController();


    AddressDao addressDao = new AddressDaoImpl();

    public String selfInfo() {


        String result = userOperate("请选择你的操作", "1:查看用户信息", "2:修改个人信息", "3:修改收货地址", "4:添加收货地址", "5:删除收货地址", "I:首页");
        if (result.equals("1")) {
            printIndividualInfo();
            return UN;
        } else if (result.equals("2")) {
            updateIndividualInfo();
            return UN;
        } else if (result.equals("3")) {
            updateAddress();
            return UN;
        } else if (result.equals("4")) {
            addAddress();
            return UN;
        } else if (result.equals("5")) {
            deleteAddress();
            return UN;
        }
        return INDEX;
    }

    public void addAddress() {
        System.out.println("请输入地址名称");
        Address address = new Address();
        address.setAddress(sc.next());
        address.setId(Entity.getUUID());
        address.setUserId(userController.getLoginUser().getId());
        address.setCreateTime(new Date());
        int i = addressDao.addAddress(address);
        if (i > 0) {
            System.out.println("添加成功");
            return;
        }
        System.out.println("添加失败");

    }


    public void updateAddress() {
        List<Address> addresses = addressDao.getAddressListByUserId(userController.getLoginUser().getId());
        printAddressInfo(addresses);
        System.out.println("请选择你要修改的地址编号");
        int i = sc.nextInt();
        Address address = addresses.get(i);

        System.out.println("请输入地址名称：无需更新的字段输入n");
        String addressName = sc.next();
        if (!"n".equals(addressName)) address.setAddress(addressName);

        System.out.println("是否设置为默认地址：yes or no");
        String b = sc.next();
        if ("yes".equals(b)) {
            address.setDefaultStatus("1");
            // 先去掉当前用户的默认地址
            addressDao.updateOldAddressDefaultStatus(userController.getLoginUser());
        } else if ("no".equals(b)) {
            address.setDefaultStatus("2");
        }

        System.out.println("address: " + address);
        int r = addressDao.updateAddress(address);

        if (r > 0) {
            System.out.println("修改收货地址成功");
            return;
        }
        System.out.println("修改个人收货地址失败");

    }


    public void deleteAddress() {
        List<Address> addresses = addressDao.getAddressListByUserId(userController.getLoginUser().getId());
        printAddressInfo(addresses);
        System.out.println("请输入要删除的地址编号：");
        int index = sc.nextInt();
        Address address = addresses.get(index);
        int i = addressDao.deleteAddressById(address);
        if (i > 0) {
            System.out.println("收货地址删除成功");
            return;
        }

        System.out.println("收货地址删除失败");

    }


    public void printIndividualInfo() {
        System.out.println("-----------------------------------------------------------------个人中心-----------------------------------------------------------------");
        User loginUser = userController.getLoginUser();
        System.out.println("ID: " + loginUser.getId() + "\t\t用户名：" + loginUser.getUsername() + "\t\t电话号码：" + loginUser.getPhone() + "\t\t余额：" + loginUser.getBalance() + "\t\t地址：" + loginUser.getAddressIds() + "\t\t邮箱：" + loginUser.getEmail() + "\t\t创建时间：" + loginUser.getCreateTime() + "\n");
        printAddressInfo();
    }


    public void printAddressInfo(List<Address> addresses) {
        int index = 0;
        System.out.println("收货地址：");
        for (Address address : addresses) {
            if ("1".equals(address.getDefaultStatus()))
                System.out.println(++index + ": " + address.getAddress() + " [默认地址]");
            else {
                System.out.println(++index + ": " + address.getAddress());
            }
        }
    }

    public void printAddressInfo() {
        List<Address> addresses = addressDao.getAddressListByUserId(userController.getLoginUser().getId());
        int index = 0;
        System.out.println("收货地址：");
        for (Address address : addresses) {
            if ("1".equals(address.getDefaultStatus()))
                System.out.println(++index + ": " + address.getAddress() + " [默认地址]");
            else {
                System.out.println(++index + ": " + address.getAddress());
            }
        }
    }
    public void updateIndividualInfo() {
        User user = userController.getLoginUser();
        System.out.println("请输入要更新的字段值，无需更新的字段输入n");
        System.out.println("请入用户名");
        String username = sc.next();
        if (!"n".equals(username)) user.setUsername(username);

        System.out.println("请输入密码");
        String password = sc.next();
        if (!"n".equals(password)) user.setPassword(password);


        System.out.println("请输入用户电话号码");
        String phone = sc.next();
        if (!"n".equals(phone)) user.setPhone(phone);


        System.out.println("请输入用户邮箱");
        String email = sc.next();
        if (!"n".equals(email)) user.setEmail(email);


        System.out.println(user);
        String s = userController.updateUser(user);
        System.out.println(s);
    }


}
